/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DAL.TourBookingBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mr-aUgust
 */
@WebServlet(name = "ControllerConfirmCanTour", urlPatterns = {"/ControllerConfirmCanTour"})
public class ControllerConfirmCanTour extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         HttpSession session=request.getSession(true);
        TourBookingBean tb=new TourBookingBean();
        String usname=session.getAttribute("User").toString();
        
        String tourBookingId=request.getParameter("CBookingid");
        String CtourId=request.getParameter("CtourId");
        String CtypeTour=request.getParameter("CtypeTour");
        String CnumberOfChild=request.getParameter("CnumberOfChild");
        String CnumberOfAdult=request.getParameter("CnumberOfAdult");
        String CtotalOfGuest=request.getParameter("CtotalOfGuest");
        
        String Ctotalmoney=request.getParameter("Ctotalmoney");
        float totalMoney=Float.parseFloat(Ctotalmoney);    
        
        String Cstartdate=request.getParameter("Cstartdate");
        String CendDate=request.getParameter("CendDate");
        String CdateBooking=request.getParameter("CdateBooking");
        String Ccash=request.getParameter("Ccash");
        
        session.setAttribute("tourBookingId",tourBookingId);
        session.setAttribute("CtourId",CtourId);
        session.setAttribute("CtypeTour",CtypeTour);
        session.setAttribute("CnumberOfChild",CnumberOfChild);
        session.setAttribute("CnumberOfAdult",CnumberOfAdult);
        session.setAttribute("CtotalOfGuest",CtotalOfGuest);
        session.setAttribute("Ctotalmoney",Ctotalmoney);
        session.setAttribute("Cstartdate",Cstartdate);
        session.setAttribute("CendDate",CendDate);
        session.setAttribute("CdateBooking",CdateBooking);
        session.setAttribute("Ccash",Ccash);
        
        int numOfday=tb.getDayOfTour(usname,tourBookingId);        
      
        double refunMoney=0;
        
        if(numOfday<=0)
        {
          out.println("<script>alert('You can't cancel this tour. Because this tour is using !');window.location='index.jsp?page=listTour';</script>");
 
        }
        else if(numOfday==1) {
            refunMoney=totalMoney*0.75;
        }
        else if(numOfday==2) {
            refunMoney=totalMoney*0.8;
        }
        else if(numOfday==3) {
            refunMoney=totalMoney*0.85;
        }
        else if(numOfday==4) {
            refunMoney=totalMoney*0.9;
        }
        else {
            refunMoney=totalMoney*0.95;
        }
        session.setAttribute("crefundMoney",refunMoney);
        response.sendRedirect("index.jsp?page=ConfirmCancelTour");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
